package system.notify;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import system.wrap_filter.Standard;

/**
 * Simple demonstration of using the javax.mail API.
 * 
 * Run from the command line. Please edit the implementation to use correct
 * email addresses and host name.
 */
public final class Emailer {
	final static String username = "chriswangh@gmail.com";
	final static String password = "82117770";
	private Standard standard;
	private String toEmail;
	private Message message;

	public Emailer(Standard standard, String toEmail) {
		this.standard = standard;
		this.toEmail = toEmail;
		this.send();
	}

	public void send() {
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
		Authenticator authenticator = new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		};
		Session session = Session.getInstance(props, authenticator);
		try {
			if (standard.getAttribute().equals("h-alert")) {
				message = new MimeMessage(session);
				message.setFrom(new InternetAddress(username));
				message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(toEmail));
				message.setSubject("Hardware Alert");
				message.setContent(standard.toString(),
						"text/html;charset=utf-8");
			} else if (standard.getAttribute().equals("s-alert")) {
				message = new MimeMessage(session);
				message.setFrom(new InternetAddress(username));
				message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(toEmail));
				message.setSubject("Software Alert");
				message.setContent(standard.toString(),
						"text/html;charset=utf-8");
			}

			// else if (standard.getAttribute().equals("h-alert")) {
			// message.setSubject("Hardware Warning");
			// message.setContent("adsf", "text/html;charset=utf-8");
			// } else if (standard.getAttribute().equals("h-alert")) {
			// message.setSubject("Software Warning");
			// message.setContent("adsf", "text/html;charset=utf-8");
			// }
			if (message != null) {
				Transport.send(message);
				System.out.println("done");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}